package TG_1160;

public class Solution {
	public int countCharacters(String[] words, String chars) {
		int[] charMap = count(chars);
		int res = 0;
		for (String word : words) {
			int[] wordMap = count(word);
			if (contains(charMap, wordMap)) {
				res+=word.length();
			}
		}
		return res;

	}

	public boolean contains(int[] charMap, int[] wordMap) {
		for (int i = 0; i < 26; i++) {
			if (charMap[i] < wordMap[i]) {
				return false;
			}
		}
		return true;
	}

	public int[] count(String chars) {
		int[] counter = new int[26];
		for (int i = 0; i < chars.length(); i++) {
			char temp = chars.charAt(i);
			counter[temp - 'a']++;
		}
		return counter;
	}
}
